package com.sicau.utils;

import com.sicau.entity.dto.TaskInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CsvUtil {

    /**
     * 导出
     *
     * @param file csv文件(路径+文件名)，csv文件不存在会自动创建
     * @param dataList 数据
     * @return
     */
    public static boolean exportCsv(File file, List<String> dataList){
        boolean isSucess=false;

        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out);
            bw =new BufferedWriter(osw);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }


    public static List<TaskInfo> csvMappingObj(MultipartFile file)throws Exception{

        List<TaskInfo> taskInfos = new ArrayList<>();

        InputStream is = file.getInputStream();
        InputStreamReader iReader = new InputStreamReader(is, "UTF-8");
        BufferedReader br = new BufferedReader(iReader);
        System.err.println("进来了");
        String line = null;
        line = br.readLine();
    //                    line.replaceAll("，", ",");
        System.out.println(line);
        line = line.substring(1);
        String[] keyWords = line.split(",");


        while((line = br.readLine()) != null){
    //                        line.replaceAll("，", ",");
            TaskInfo temp = new TaskInfo();
            String[] contexts = line.split(",");
            int kl = keyWords.length,cl = contexts.length;
            if(kl != cl){
                taskInfos.clear();
                return null;//检测到有空的字段，结束导入操作
            }

            for (int i=0;i<keyWords.length;i++){


                contexts[i] = contexts[i].replaceAll("\"","");
                switch (keyWords[i]){
                    case "创作者编号":{
                        temp.setComposerId(contexts[i].trim());
                        break;
                    }
                    case "结束时间": {
                        temp.setEndTime(contexts[i].trim());
                        break;
                    }
                    case "总计": {
                        temp.setSettlementAmount(contexts[i].trim());
                        break;
                    }
                    case "开始时间": {
                        temp.setStartTime(contexts[i].trim());
                        break;
                    }
                    case "任务内容": {
                        temp.setTaskContext(contexts[i].trim());
                        break;
                    }
                    case "项目编号": {
                        break;
                    }
                    case "项目名称": {
                        temp.setTaskName(contexts[i].trim());
                        break;
                    }
                    case "项目状态": {
                        temp.setTaskStatus(contexts[i].trim());
                        break;
                    }
                    default:{
                            taskInfos.clear();
                            return null;//检测到不能识别的字段名，结束导入操作
                        }
                }


    //                    System.out.println(keyWords[i] + ":" + contexts[i]);
            }

            temp.setTaskId(UUID.randomUUID().toString().replaceAll("-",""));

            System.err.println(temp);

            taskInfos.add(temp);
//            System.err.println(temp.getComposerId()+"-" + temp.getTaskName());

        }
        return taskInfos;

    }




}
